package com.gym.code.config.admin.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.gym.code.config.admin.bean.ColumnMappingBean;
import com.gym.code.config.admin.dao.IColumnMappingDao;

/**
 * 
 * @author xinchun.wang
 * @email: 532002108@qq.com
 * @createTime 2015-4-11 下午5:24:22
 */
@Repository
public class ColumnMappingDao implements IColumnMappingDao {

	private static final String insertSql = "insert into column_mapping_info (table_id,property_name,column_name,value_type,is_enum,enum_type_name,pattern_value) values (?,?,?,?,?,?,?)";
	private static final String selectSql = "SELECT id,table_id,property_name,column_name,value_type,is_enum,enum_type_name,pattern_value FROM column_mapping_info ";
	private static final String updateSql = "update column_mapping_info set property_name =?,is_enum = ?,enum_type_name =?,pattern_value = ? where id = ? ";

	@Autowired
	@Qualifier("jdbcTemplate")
	private JdbcTemplate jdbcTemplate;

	@Override
	public int insert(ColumnMappingBean bean) {
		Object[] args = new Object[] { bean.getTableId(),
				bean.getPropertyName(), bean.getColumnName(),
				bean.getValueType().getJavaName(), bean.getEnumFlag(),bean.getEnumTypeName(),bean.getPatternValue() };
		return jdbcTemplate.update(insertSql, args);
	}

	@Override
	public ColumnMappingBean query(long id) {
		List<ColumnMappingBean> result = jdbcTemplate.query(selectSql
				+ " where id = ?", new RowMapper<ColumnMappingBean>() {
			@Override
			public ColumnMappingBean mapRow(ResultSet rs, int rowNum) throws SQLException {
				ColumnMappingBean bean = new ColumnMappingBean();
				bean.setId(rs.getLong("id"));
				bean.setTableId(rs.getLong("table_id"));
				bean.setPropertyName(rs.getString("property_name"));
				bean.setColumnName(rs.getString("column_name"));
				bean.setValueType(rs.getString("value_type"));
				bean.setEnumFlag(rs.getInt("is_enum"));
				bean.setEnumTypeName(rs.getString("enum_type_name"));
				bean.setPatternValue(rs.getString("pattern_value"));
				return bean;

			}
		}, id);
		if (CollectionUtils.isEmpty(result)) {
			return null;
		}
		return result.get(0);
	}

	@Override
	public int update(ColumnMappingBean bean) {
		Object[] args = new Object[] { bean.getPropertyName(),
			 bean.getEnumFlag(),bean.getEnumTypeName(),bean.getPatternValue(),bean.getId()};
		return jdbcTemplate.update(updateSql, args);
	}

	@Override
	public List<ColumnMappingBean> queryByTableId(long tableId) {
		List<ColumnMappingBean> result = jdbcTemplate.query(selectSql
				+ " where table_id = ?", new RowMapper<ColumnMappingBean>() {
			@Override
			public ColumnMappingBean mapRow(ResultSet rs, int rowNum) throws SQLException {
				ColumnMappingBean bean = new ColumnMappingBean();
				bean.setId(rs.getLong("id"));
				bean.setTableId(rs.getLong("table_id"));
				bean.setPropertyName(rs.getString("property_name"));
				bean.setColumnName(rs.getString("column_name"));
				bean.setValueType(rs.getString("value_type"));
				bean.setEnumFlag(rs.getInt("is_enum"));
				bean.setEnumTypeName(rs.getString("enum_type_name"));
				bean.setPatternValue(rs.getString("pattern_value"));
				return bean;

			}
		}, tableId);
		if (CollectionUtils.isEmpty(result)) {
			return Collections.emptyList();
		}
		return result;
	}

	@Override
	public List<ColumnMappingBean> query(List<Long> idList) {
		List<ColumnMappingBean> result = jdbcTemplate.query(selectSql
				+ " where id in ("+ StringUtils.join(idList,',')+")", new RowMapper<ColumnMappingBean>() {
			@Override
			public ColumnMappingBean mapRow(ResultSet rs, int rowNum) throws SQLException {
				ColumnMappingBean bean = new ColumnMappingBean();
				bean.setId(rs.getLong("id"));
				bean.setTableId(rs.getLong("table_id"));
				bean.setPropertyName(rs.getString("property_name"));
				bean.setColumnName(rs.getString("column_name"));
				bean.setValueType(rs.getString("value_type"));
				bean.setEnumFlag(rs.getInt("is_enum"));
				bean.setEnumTypeName(rs.getString("enum_type_name"));
				bean.setPatternValue(rs.getString("pattern_value"));
				return bean;

			}
		});
		if (CollectionUtils.isEmpty(result)) {
			return Collections.emptyList();
		}
		return result;
	}

	@Override
	public void insert(List<ColumnMappingBean> list) {
		for(ColumnMappingBean item : list){
			this.insert(item);
		}
	}

}
